package com.yinet.web.controller.yctk;

import com.yinet.common.core.controller.BaseController;
import com.yinet.common.core.domain.AjaxResult;
import com.yinet.common.core.page.TableDataInfo;
import com.yinet.common.utils.StringUtils;
import com.yinet.generator.domain.YinetYctk;
import com.yinet.yyqh.service.PortService;
import com.yinet.yyqh.service.Yctkervice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * app接口
 *
 * @author yinet
 * @date 2019-09-02
 */
@Controller
@RequestMapping("/port")
public class PortController extends BaseController {

    @Autowired
    private PortService portService;
    @Autowired
    private Yctkervice yctkService;

    //登录
    @PostMapping("/login")
    @ResponseBody
    public AjaxResult login(String card,String passWord) {
        if(StringUtils.isNull(card)||StringUtils.isNull(passWord)){
            return AjaxResult.error("用户名或密码为空");
        }
        List<YinetYctk> yctk=portService.selectCard(card);
        System.err.println(yctk);
        if(yctk.isEmpty()){
            return  error("用户名或密码错误");
        }
        String sfzCard=yctk.get(0).getSfzcard().substring(yctk.get(0).getSfzcard().length()-6);
        if(sfzCard.equals(passWord)){
            AjaxResult aj = new AjaxResult(AjaxResult.Type.SUCCESS,null);
            aj.put("username", yctk.get(0).getUsername());
            aj.put("phone", yctk.get(0).getPhone());
            aj.put("code", "200");
            return aj;
        }else{
            return error("用户名或密码错误");
        }
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo login(String card) {
        List<YinetYctk> yctk=portService.selectCard(card);
        for (YinetYctk y:yctk){
           //查询不通过理由
           y.setSmsContent(portService.selectNotPassReason(y.getUuid()));

        }
        return getDataTabla(yctk);
    }

    //添加定位
    @PostMapping("/addSite")
    @ResponseBody
    public AjaxResult addSite(String uuid,String ycSite,String x,String y) {
        YinetYctk yctk=new YinetYctk();
        yctk.setUuid(uuid);
        yctk.setYc_site(ycSite);
        yctk.setYcSiteX(x);
        yctk.setYcSiteY(y);
        yctk.setSite_time(new Date());
        int i=portService.insertYctkSite(yctk);
        AjaxResult aj = new AjaxResult(AjaxResult.Type.SUCCESS,null);
        if (i>0){
            aj.put("code",200);
        }else{
            aj.put("code",500);
        }
        return aj;
    }

    //修改踏勘状态
    @PostMapping("/TkStatus")
    @ResponseBody
    public AjaxResult TkStatus(String card,String uuid) {
        AjaxResult aj = new AjaxResult(AjaxResult.Type.SUCCESS,null);
        List<YinetYctk> list=portService.selectCard(card);
        for (YinetYctk yctk:list){
            if(yctk.getIsfinish().equals("3")){
                aj.put("error", "此账号正在踏勘");
                aj.put("code", "500");
                return aj;
            }
        }
        yctkService.updateIsfinish(uuid);
        aj.put("success", "更改成功");
        aj.put("code", "200");
        return aj;
    }

    //查找正在踏勘中的事件
    @PostMapping("/BebeingTk")
    @ResponseBody
    public TableDataInfo BebeingTk(String card) {
        //查找正在踏勘中的事件
        List<YinetYctk> list=portService.selectBebeingTk(card);
        return getDataTabla(list);
    }


    //查找正在踏勘中的事件
    @PostMapping("/QyName")
    @ResponseBody
    public AjaxResult QyName(String card) {
        //查找正在踏勘中的事件
        System.err.println("card:"+card);
        YinetYctk yctk=portService.selectCardQyName(card);
        System.err.println("111yctk:"+yctk);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("QyName", yctk.getQyName());
        return ajax;
    }
}
